class controller{
	public static elevator e1 = new elevator(1);
	public static elevator e2 = new elevator(2);
	public static elevator e3 = new elevator(3);
	private static int xxx= 0;
	private static elevator[] ch = new elevator[7];
	private static String dir="stay";

	static
	{
		for(int i=0;i<4;i++) ch[i] = null;
	}

	public static void callOn(int x){
		if(xxx==0) return;
		ch[xxx].setTarFloor(x);
		//ch[xxx].move();
		new thread(ch[xxx]).start();
		ch[xxx] = null;
		xxx = 0;
		dir="stay";
	}
	public static void callOn(String s,int x){
		xxx = x;
		dir = s;
		choose();
	}
	
	private static void choose(){ //控制分发线程
		int ch1 = judge(e1,dir);
		int ch2 = judge(e2,dir);
		int ch3 = judge(e3,dir);
		if(ch1==1) start(e1,1);
		else if(ch2==1) start(e2,2);
		else if(ch3==1) start(e3,3);
		else if(ch1==0) start(e1,1);
		else if(ch2==0) start(e2,2);
		else if(ch3==0) start(e3,3);
		else stayForAWhile();
		dir="stay";
	}
	private static void stayForAWhile(){
		sleep(2000);
		choose();
	}
	private static int judge(elevator e,String dir){
		System.out.println("judge!");
		if(e.direction == "stay") return 0;
		if(e.direction !=dir) return -1;
		else return 1;

	}
	private static void start(elevator e,int x){
		ch[xxx] = e;
		if(e.isRun()) e.setStay(xxx);
		else {
			e.setTarFloor(xxx);
			//e.move();
			new thread(e).start();
		}
		
	}
	private static void sleep(int x){
		try{
			Thread.sleep(x);
		}
		catch(Exception e){
			System.out.println("sleep error!");
		}
	}


}